package com.macrosolution.exdebito


import static org.springframework.http.HttpStatus.*
import grails.plugin.springsecurity.annotation.Secured;
import grails.transaction.Transactional

/**
 * DocTypeController
 * A controller class handles incoming web requests and performs actions such as redirects, rendering views and so on.
 */
@Secured(['ROLE_ADMIN','ROLE_CC'])
class DocTypeController {

	static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

	def index(Integer max) {
		params.max = Math.min(max ?: 10, 100)
		respond DocType.list(params), model:[docTypeInstanceCount: DocType.count()]
	}

	def list(Integer max) {
		params.max = Math.min(max ?: 10, 100)
		respond DocType.list(params), model:[docTypeInstanceCount: DocType.count()]
	}

	def show(DocType docTypeInstance) {
		respond docTypeInstance
	}

	def create() {
		respond new DocType(params)
	}

	@Transactional
	def save(DocType docTypeInstance) {
		if (docTypeInstance == null) {
			notFound()
			return
		}

		if (docTypeInstance.hasErrors()) {
			respond docTypeInstance.errors, view:'create'
			return
		}

		docTypeInstance.save flush:true

		request.withFormat {
			form {
				flash.message = message(code: 'default.created.message', args: [
					message(code: 'docTypeInstance.label', default: 'DocType'),
					docTypeInstance.id
				])
				redirect docTypeInstance
			}
			'*' { respond docTypeInstance, [status: CREATED] }
		}
	}

	def edit(DocType docTypeInstance) {
		respond docTypeInstance
	}

	@Transactional
	def update(DocType docTypeInstance) {
		if (docTypeInstance == null) {
			notFound()
			return
		}

		if (docTypeInstance.hasErrors()) {
			respond docTypeInstance.errors, view:'edit'
			return
		}

		docTypeInstance.save flush:true

		request.withFormat {
			form {
				flash.message = message(code: 'default.updated.message', args: [
					message(code: 'DocType.label', default: 'DocType'),
					docTypeInstance.id
				])
				redirect docTypeInstance
			}
			'*'{ respond docTypeInstance, [status: OK] }
		}
	}

	@Transactional
	def delete(DocType docTypeInstance) {

		if (docTypeInstance == null) {
			notFound()
			return
		}

		docTypeInstance.delete flush:true

		request.withFormat {
			form {
				flash.message = message(code: 'default.deleted.message', args: [
					message(code: 'DocType.label', default: 'DocType'),
					docTypeInstance.id
				])
				redirect action:"index", method:"GET"
			}
			'*'{ render status: NO_CONTENT }
		}
	}

	protected void notFound() {
		request.withFormat {
			form {
				flash.message = message(code: 'default.not.found.message', args: [
					message(code: 'docTypeInstance.label', default: 'DocType'),
					params.id
				])
				redirect action: "index", method: "GET"
			}
			'*'{ render status: NOT_FOUND }
		}
	}
	def addFolder(){
		
		def type= params.documentType

		DocType findFolder = DocType.findByDocumentTypeAndDocumentCategory(type,1)
		if(findFolder){

			//			flash.message = message(code: 'Esite gia una cartella con questo nome')
			//			redirect controller:'fileDocument',action:'fileHome'
			response.status = 401
			render message(code: 'Esite gia una cartella con questo nome')

		}else{

			DocType dt = new DocType(params)
			dt.documentCategory=1
			if(dt.save(flush:true)){
				File folder = new File(grailsApplication.config.uploadFolder+File.separator+"Cloud"+File.separator+dt.documentType)
				if(folder.mkdirs()){
					def allFolders = DocType.findAllByDocumentCategory(1)
					render template:'/fileDocument/folderTree', model:[folders:allFolders]
				}else{
					response.status = 401
					render message(code: 'Non riesco a creare la cartella')
				}
			}
			else{
				response.status = 401
				render message(code: 'Devi specificare un nome per la cartella')
				//				flash.message = message(code: 'Devi specificare un nome per la cartella',)
				//				redirect controller:'fileDocument',action:'fileHome'
			}
		}
	}
	@Secured(['ROLE_ADMIN','ROLE_AMM','ROLE_CC','ROLE_AVV','ROLE_C','ROLE_SC','ROLE_BCKOFF'])
	def files_number(DocType folder){
		int numbers=FileDocument.countByDocumentType(folder);
		render numbers;
	}
}
